<html>
<head>
    <title>吉垚物业后台管理系统</title>
    <meta charset="utf-8">
    <link rel="shortcut icon" th:href="./images/favicon.ico" type="image/x-icon">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="format-detection" content="telephone=no">
    <!-- 引入css -->
    <link rel="stylesheet" href="./css/plugins/font-awesome-4.7.0/css/font-awesome.min.css"  media="all">
    <link rel="stylesheet" href="./lib/layui-v2.3.0/css/layui.css"  media="all">
    <link rel="stylesheet" href="./css/main.css"  media="all">
    <link rel="stylesheet" href="./css/bootstrap.min.css">
</head>
<body>
<div class="">
    <form action="#">
        <div class="layui-form-item">
            <label class="layui-form-label">标题</label>
            <div class="layui-input-inline">
                <input type="text" name="topic" id="topic" lay-verify="title" autocomplete="off" placeholder="请输入文章标题" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">封面</label>
            <div class="layui-input-inline">
                <input type="file" name="img" id="img" lay-verify="title" autocomplete="off"class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">原封面</label>
            <div class="layui-input-inline" id="imgDB">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">内容</label>
            <div class="layui-input-inline">
                <textarea type="text" name="content" style="height: 100px" id="content" lay-verify="title" autocomplete="off" placeholder="请输入文章内容" class="layui-input"></textarea>
            </div>
        </div>
        <div >
            <label class="layui-form-label">选择框</label>
            <div class="layui-input-inline">
                <select name="interest" id="types" lay-filter="aihao">
                </select>
            </div>
        </div>
        <hr>
        <div class="layui-form-item timo-finally">
            <button class="layui-btn ajax-submit" onclick="updateText()"><i class="fa fa-check-circle"></i> 保存</button>
            <button class="layui-btn btn-secondary close-popup" id="close"><i class="fa fa-times-circle"></i> 关闭</button>
        </div>
    </form>
</div>
<script src="./lib/layui-v2.3.0/layui.js" charset="utf-8"></script>
<script src="./js/main.js" charset="utf-8"></script>
<script src="js/plugins/jquery-3.3.1.min.js"></script>
<script src="js/checkToken.js"></script>
</body>
<script type="text/javascript">
    // var category=window.location.search;
    // //截取，获取获得category的具体值
    // var typeid = category.substring(category.lastIndexOf('=')+1, category.length);
    // console.log(category);
    // console.log(cateval);

    var textid=sessionStorage.getItem('textid');
    var typeid=sessionStorage.getItem('typeid');
    var imgid=sessionStorage.getItem('imgid');
    var topic=sessionStorage.getItem('topic');
    var content=sessionStorage.getItem('content');
    // var userid=sessionStorage.getItem('userid');
    var imgname=sessionStorage.getItem('imgname');

    $.ajax({url:"http://localhost:8081/admin/getImg.do?imgname="+imgname,
        headers:{
            accessToken:localStorage.getItem('accessToken')
    },
        success:function (data) {
            $('#imgDB').append('<br><img src="data:img/png;base64,'+data+'" class="img-circle" style="width: 100px;height: 100px">');
        }
    })

    $.ajax({
        url:'http://localhost:8081/admin/getTypes.do',
        headers:{
            accessToken:localStorage.getItem('accessToken')
        },
        success:function (data) {
            checkToken(data)
            console.log(data.result);
            for (var i=0;i<data.result.length;i++) {
                console.log("id:"+data.result[i]['id']+'and typeid:'+typeid);
                if (data.result[i]['id']==typeid)
                    $('#types').append('<option value="'+data.result[i]['id']+'" selected>'+data.result[i]['name']+'</option>')
                else
                    $('#types').append('<option value="'+data.result[i]['id']+'">'+data.result[i]['name']+'</option>')
            }
        }
    })

    $('#topic').val(topic);
    $('#content').val(content);
    function updateText() {
        topic=$('#topic').val();
        content=$('#content').val();
        typeid=$('#types').val();
        var img=document.getElementById('img').files[0];

        if (topic==''){
            alert('标题不能为空');
            return;
        }
        var formData = new FormData();
        formData.append('id',textid);
        formData.append('topic',topic);
        formData.append('content',content);
        formData.append('typeid',typeid);
        // formData.append('userid',userid);
        formData.append('img',img);

        $.ajax({
            url:'http://localhost:8081/admin/updateText.do',
            headers:{
                accessToken:localStorage.getItem('accessToken')
            },
            type: "post",
            data: formData,
            contentType: false,
            processData: false,
            success:function (data) {
                checkToken(data)
                if (data.resMsg!='更新失败'){
                    top.location.reload();
                    this.close();
                }else{
                    alert('更新失败');
                }
            }
        })
    }

</script>
</html>